import {
	objDeepCopy,
	getCascaderObj,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {
			this.Model.mainId = this.$route.query.id;
			this.Model.mainType = this.$route.query.type;
			this.getDetail(this, undefined, this.$route.query.id);
		},
		//获取详情 
		getDetail(that, page, whInventoryMainId) {
			var apiTest = this.apiTest;
			page = page || this.Model.page;
			this.$ajax({
				method: 'post',
				url:apiTest+'/supplier/whInventoryDetails/list',
				// url: '/api/whInventoryDetails/list',
				data: {
					condition: {
						whInventoryMainId: whInventoryMainId
					},
					pageIndex: page.pageIndex,
					pageSize: page.pageSize,
				}
			}).then((res) => {
				that.Model.lotNumberArr = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					var pagination = commonObject.paginationFunction(result);
					that.Model.paginationObject = objDeepCopy(pagination);
					if (result) {
						that.Model.fiNum = result.hasBeentotal;
						that.Model.allNum = result.total;
						that.Model.orderStatus = result.inventoryStatus;//盘点单的状态
						that.Model.refundStep.activeStep = result.inventoryStatus - 1;
						that.Model.tableFormInline = [{
								label: '盘点单号',
								value: result.countingNumber
							},
							{
								label: 'sap盘点单号',
								value: result.sapCountingNumber
							},
							{
								label: '盘点仓库',
								value: result.inventoryWarehouse
							},
							{
								label: '创建人',
								value: result.createBy
							},
							{
								label: '开始盘点时间',
								value: result.inventoryStartTime
							}
						]
						//表格数据
						if (result.detailsList) {
							that.Model.tableData = objDeepCopy(result.detailsList);
						}
						for (let i = 0; i < result.detailsList.length; i++) {
							that.Model.lotNumberArr.push(result.detailsList[i].lotNumber);
						}

					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//扫码
		onSearchCode() {
			var apiTest = this.apiTest;
			var that = this;
			var code = this.Model.searchCode;
			var id = this.Model.mainId;
			var fiNum = this.Model.fiNum;
			if (code.length == 0) {
				that.tips(that, 'warning', '填写或扫描条码');
			}
			this.$ajax({
				method: 'post',
				// url: '/api/whInventoryDetails/accordingToBarcodeComparison/' + fiNum,
				url: apiTest+'/supplier/whInventoryDetails/accordingToBarcodeComparison/' + fiNum,
				data: {
					keyword: code,
					whInventoryMainId: id
				}
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					if (result.inventoryDetails) {
						that.Model.fiNum = result.totalNumberOfSolids;
						that.lotNumberRes(that, result.inventoryDetails);
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//判断条码存在 置顶
		lotNumberRes(that, obj) {
			let lotNumber = obj.lotNumber;

			let lotNumberArr = objDeepCopy(that.Model.lotNumberArr);
			let tableData = objDeepCopy(that.Model.tableData);
			let index = lotNumberArr.indexOf(lotNumber);
			if (index === -1) {
				tableData.unshift(obj);
				lotNumberArr.push(lotNumber);
			} else {
				let item = tableData.splice(index, 1);
				tableData.unshift(obj);
				//
				let its = lotNumberArr.splice(index, 1);
				lotNumberArr.unshift(its[0]);
			}

			that.Model.tableData = objDeepCopy(tableData);
			that.Model.lotNumberArr = objDeepCopy(lotNumberArr);
		},
		//保存或者 结束盘点
		saveOrEndAjax(that, whetherToEnd) {
			var apiTest = this.apiTest;
			var id = this.Model.mainId;
			var obj;
			var params = [];
			var list = this.Model.tableData;
			for (let i = 0; i < list.length; i++) {
				obj = {
					id: list[i].id, //盘点详情表ID
					countingResults: list[i].countingResults, //盘点结果  1.盘盈 2.无差异 3盘亏
					numberOfDiscs: list[i].numberOfDiscs, //应盘数
					numberOfSolidOrders: list[i].numberOfSolidOrders, //实盘数
					whInventoryMainId: id, //盘点主表ID
					countingRecord: list[i].countingRecord //盘点记录
				}
				params.push(obj)
			}
			//ajax
			this.$ajax({
				method: 'post',
				// url: '/supplier/whInventoryDetails/saveCountData/' + whetherToEnd, //whetherToEnd:0,1(0:保存，1：结束)
				url: apiTest+'/supplier/whInventoryDetails/saveCountData/' + whetherToEnd, //whetherToEnd:0,1(0:保存，1：结束)
				data: params
			}).then((res) => {
				// that.Model.tableData = [];
				var data = res.data;
				if (data.status == 200) {
					that.tips(that, 'success', data.result);
					setTimeout(function(){
						that.init();
						that.Model.mainType = 'detail';
					},1000)
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//点击 保存
		onSave() {
			this.saveOrEndAjax(this, 0)
		},
		//点击 结束盘点
		onEnd() {
			this.saveOrEndAjax(this, 1)
		},
		//点击 提交盘点报告
		onSubmit() {
			var that = this;
			var id = this.Model.mainId;
			var submitter = 1;
			var params = this.Model.tableData;
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest+'/api/whInventoryDetails/submitInventoryReport/' + id + '/' + submitter,
				// url: '/api/whInventoryDetails/submitInventoryReport/' + id + '/' + submitter,
			}).then((res) => {
				// that.Model.tableData = [];
				var data = res.data;
				if (data.status == 200) {
					that.tips(that, 'success', data.result);
					setTimeout(function(){
						that.init();
						that.Model.mainType = 'detail';
					},1000)
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//
		dipStart() {
			this.Model.mainType = 'dip'
		},
		//tab 切换
		onTabChange() {

		},
		//返回上一页
		back() {
			this.$router.push({
				path: 'dipInventory',
				query: {

				}
			});
		},

		//
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
		toggleTableDetail(val) {

		},
		handleSelectionChange(val) {

		},
		handleCurrentChange(val) {

		},
		handleSizeChange(val) {

		},
		cardClick(color) {

		},
		showChild(val) {

		},
		toSearch(val) {

		},
		toClear(val) {

		},
		toExport(val) {
			console.log(val);
		},



	},
	watch: {
		//
		$route: {
			handler(newValue, oldValue) {
				if (newValue.query.id) {
					this.Model.mainId = this.$route.query.id;
					this.Model.mainType = this.$route.query.type;
					this.getDetail(this, undefined, this.$route.query.id);
				}
			},
			deep: true
		},

	},



}
export default Controller;
